package com.rybbaby.tss.front.tssMemberAttendClassRecord.service.imp;

import com.google.common.collect.ImmutableMap;
import com.rybbaby.tss.core.bean.PageBean;
import com.rybbaby.tss.core.utils.SequenceUtil;
import com.rybbaby.tss.core.utils.StringUtils;
import com.rybbaby.tss.core.web.service.imp.BaseServiceImp;
import com.rybbaby.tss.front.tssClassAttendance.dao.ITssClassAttendanceDao;
import com.rybbaby.tss.front.tssClassAttendance.entity.TssClassAttendance;
import com.rybbaby.tss.front.tssClasses.dao.ITssClassesDao;
import com.rybbaby.tss.front.tssClassesLessons.dao.ITssClassesLessonsDao;
import com.rybbaby.tss.front.tssClassesLessons.entity.TssClassesLessons;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.dao.ITssMemberAttendClassRecordDao;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.entity.TssMemberAttendClassRecord;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.service.ITssMemberAttendClassRecordService;
import com.rybbaby.tss.front.tssMemberCheckAttendance.dao.ITssMemberCheckAttendanceDao;
import com.rybbaby.tss.front.tssMemberCheckAttendance.entity.TssMemberCheckAttendance;
import com.rybbaby.tss.front.tssMemberPackage.dao.ITssMemberPackageDao;
import com.rybbaby.tss.front.tssMemberPackage.entity.TssMemberPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author 雷春琼
 * @description 会员报班记录表数据库操作服务实现
 * @createDate 2017-08-24 下午2:43:28
 */
@Service
@Transactional
public class TssMemberAttendClassRecordServiceImp extends BaseServiceImp<TssMemberAttendClassRecord> implements ITssMemberAttendClassRecordService {

    @Autowired
    private ITssMemberAttendClassRecordDao dao;
    @Autowired
    private ITssMemberPackageDao tssMemberPackageDao;
    @Autowired
    private ITssClassesLessonsDao tssClassesLessonsDao;
    @Autowired
    private ITssClassAttendanceDao tsClassAttendanceDao;
    @Autowired
    private ITssMemberCheckAttendanceDao tssMemberCheckAttendanceDao;
    @Autowired
    private ITssClassesDao tssClassesDao;

    /**
     * 根据会员课时包和班级id查询已报班信息
     *
     * @param memberPackageId
     * @param classId
     * @return
     */
    @Override
    public List<Map<String, Object>> classDateList(String memberPackageId, String classId) {
        return dao.classDateList(memberPackageId, classId);
    }

    /**
     * 根据会员课时包id分组查询会员报班记录
     *
     * @param memberPackageId
     * @return
     */
    @Override
    public PageBean tssMemberAttendClassRecordListGroupByClass(String memberPackageId, Integer pageNo, Integer pageSize) {

        return dao.tssMemberAttendClassRecordListGroupByClass(memberPackageId, pageNo, pageSize);
    }

    @Override
    public List<Map<String, Object>> getMemberAttendClassDetailList(String memberId, String memberPackageId, String classId) {
        return dao.getMemberAttendClassDetailList(memberId, memberPackageId, classId);
    }

    @Override
    public List<Map<String, Object>> getAttendClassLessonsDetailList(String memberId, Integer type) {

        List<Map<String, Object>> classListMap = this.dao.getAttendClassDetailList(memberId, type);
        for (Map<String, Object> map : classListMap) {
            List<Map<String, Object>> lessonListMap = this.dao.getAttendClassLessonsDetailList(memberId, map.get("id").toString());
            map.put("lessonList", lessonListMap);
        }
        return classListMap;
    }

    /**
     * 报班记录及考勤记录生成表
     *
     * @param entity
     * @param targetDataId
     * @param memberPackageId
     * @param isUpdateMemberPackage
     * @return
     * @throws Exception
     */
    @Override
    public boolean saveEntity(TssMemberAttendClassRecord entity, String targetDataId, String memberPackageId, boolean isUpdateMemberPackage) throws Exception {
        boolean bool = true;
        String ss[] = StringUtils.split(targetDataId, ",");
        String lessons = StringUtils.join(ss, "','");
        TssMemberPackage memberPackage = tssMemberPackageDao.get(TssMemberPackage.class, memberPackageId);
        List<TssMemberAttendClassRecord> tssMemberAttendClassRecordList = new ArrayList<>();
        List<TssClassesLessons> classesLessonsList = tssClassesLessonsDao.findListBySqlAndOrder(lessons, "classTime", "ASC");
        List<TssMemberCheckAttendance> tssMemberCheckAttendanceList = new ArrayList<>();
        int surplusFormalMemberPackager = 0;
        int surplusGiftMemberPackager = 0;
        int selectedClassHour = 0;
        int surplusFormalClassHour = 0;
        int surplusPresentClassHour = 0;
        boolean isNotTransit = memberPackage.getType() == 1 ? true : false;
        TssMemberCheckAttendance tssMemberCheckAttendance = new TssMemberCheckAttendance();
        //List<TssClassAttendance> tssClassAttendanceArrayList = new ArrayList<>();
        //TssClassAttendance tssClassAttendance = new TssClassAttendance();
        if (isNotTransit) {
            surplusFormalMemberPackager = memberPackage.getSurplusFormalClassHour();
            surplusGiftMemberPackager = memberPackage.getSurplusPresentClassHour();
        } else {
            surplusFormalMemberPackager = memberPackage.getSurplusFormalAttendanceDay();
            surplusGiftMemberPackager = memberPackage.getSurplusPresentAttendanceDay();
        }

        //定义报班统计消耗 剩余课时,已选班课时,剩余正课,剩余赠课
        for (TssClassesLessons lesson : classesLessonsList) {
            entity = new TssMemberAttendClassRecord();
            Integer expendClassHour = this.tssClassesDao.getExpendClassHour(lesson.getId());
            entity.setId(SequenceUtil.getUpperCaseUUID());
            entity.setMemberId(memberPackage.getMemberId());
            entity.setLessonsId(lesson.getId());
            entity.setStatus(1);
            //判断剩余课时
            if (surplusFormalMemberPackager > 0) {
                int remainder = surplusFormalMemberPackager - expendClassHour;
                if (remainder >= 0) {
                    entity.setFormalClass(expendClassHour);
                    surplusFormalMemberPackager -= expendClassHour;
                    entity.setGiftClass(0);
                } else {
                    entity.setFormalClass(surplusFormalMemberPackager);
                    entity.setGiftClass(remainder * -1);
                    surplusFormalMemberPackager = 0;
                }
            } else {
                entity.setFormalClass(0);
                entity.setGiftClass(expendClassHour);
                surplusGiftMemberPackager -= expendClassHour;
            }
            if (surplusGiftMemberPackager < 0) {
                throw new Exception("剩余课时不足");
            }
            entity.setIsReturnSection(false);
            entity.setIsAttendClass(false);
            entity.setMemberPackageId(memberPackageId);
            entity.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));
            tssMemberAttendClassRecordList.add(entity);
            //获取每节课时消耗课时数

            surplusFormalClassHour += entity.getFormalClass();
            surplusPresentClassHour += entity.getGiftClass();
            //判断正课是否已选完
            /*if (surplusFormalClassHour >= memberPackage.getSurplusFormalClassHour()) {
                surplusPresentClassHour += expendClassHour;
            }*/
            selectedClassHour += expendClassHour;
            //生成空白考勤
            tssMemberCheckAttendance = new TssMemberCheckAttendance();
            tssMemberCheckAttendance.setId(SequenceUtil.getUpperCaseUUID());
            tssMemberCheckAttendance.setMemberId(memberPackage.getMemberId());
            tssMemberCheckAttendance.setClassId(lesson.getClassId());
            tssMemberCheckAttendance.setAttendClassId(entity.getId());
            tssMemberCheckAttendance.setType(memberPackage.getType());
            tssMemberCheckAttendance.setAttendenceStatus(0);
            tssMemberCheckAttendance.setFormalClass(entity.getFormalClass());
            tssMemberCheckAttendance.setGiftClass(entity.getGiftClass());
            tssMemberCheckAttendance.setCreateTime(lesson.getClassTime());
            tssMemberCheckAttendance.setUpdateTime(new java.sql.Timestamp(System.currentTimeMillis()));
            tssMemberCheckAttendanceList.add(tssMemberCheckAttendance);
            //生成班级考勤
            /*tssClassAttendance = new TssClassAttendance();
            tssClassAttendance.setId(SequenceUtil.getUpperCaseUUID());
            tssClassAttendance.setAttendanceStatus(1);
            tssClassAttendance.setClassId(tssClassesLessons.getClassId());
            tssClassAttendance.setLessonsId(tssClassesLessons.getId());
            tssClassAttendance.setCreateTime(new java.sql.Timestamp(System.currentTimeMillis()));
            tssClassAttendanceArrayList.add(tssClassAttendance);*/
        }
        //剩余课时数,剩余正课时,剩余增课时
        if (isUpdateMemberPackage) {
            if (isNotTransit) {
                Integer SYsurplusClassHour = memberPackage.getSurplusClassHour() - selectedClassHour;
                selectedClassHour = (memberPackage.getSelectedClassHour() == null ? 0 : memberPackage.getSelectedClassHour()) + selectedClassHour;
                Integer unusedClassHour = (memberPackage.getUnusedClassHour() == null ? 0 : memberPackage.getUnusedClassHour()) + selectedClassHour;
                Integer positiveUnusedClassHour = (memberPackage.getPositiveUnusedClassHour() == null ? 0 : memberPackage.getPositiveUnusedClassHour()) + surplusFormalClassHour;
                Integer giftUnusedClassHour = (memberPackage.getGiftUnusedClassHour() == null ? 0 : memberPackage.getGiftUnusedClassHour()) + surplusPresentClassHour;
                //修改会员课时包课时相关信息
                memberPackage.setSurplusClassHour(SYsurplusClassHour);
                memberPackage.setSurplusFormalClassHour(surplusFormalMemberPackager);
                memberPackage.setSurplusPresentClassHour(surplusGiftMemberPackager);
                memberPackage.setSelectedClassHour(selectedClassHour);
                memberPackage.setUnusedClassHour(unusedClassHour);
                memberPackage.setPositiveUnusedClassHour(positiveUnusedClassHour);
                memberPackage.setGiftUnusedClassHour(giftUnusedClassHour);
                memberPackage.setIsAttendClass(true);
            } else {
                Integer SYsurplusClassHour = memberPackage.getSurplusAttendanceDay() - selectedClassHour;
                selectedClassHour = (memberPackage.getSelectedAttendanceDay() == null ? 0 : memberPackage.getSelectedAttendanceDay()) + selectedClassHour;
                Integer unusedClassHour = (memberPackage.getUnusedAttendanceDay() == null ? 0 : memberPackage.getUnusedAttendanceDay()) + selectedClassHour;
                Integer positiveUnusedClassHour = (memberPackage.getPositiveUnusedAttendanceDay() == null ? 0 : memberPackage.getPositiveUnusedAttendanceDay()) + surplusFormalClassHour;
                Integer giftUnusedClassHour = (memberPackage.getGiftUnusedAttendanceDay() == null ? 0 : memberPackage.getGiftUnusedAttendanceDay()) + surplusPresentClassHour;
                //修改会员课时包课时相关信息
                memberPackage.setSurplusAttendanceDay(SYsurplusClassHour);
                memberPackage.setSurplusFormalAttendanceDay(surplusFormalMemberPackager);
                memberPackage.setSurplusPresentAttendanceDay(surplusGiftMemberPackager);
                memberPackage.setSelectedAttendanceDay(selectedClassHour);
                memberPackage.setUnusedAttendanceDay(unusedClassHour);
                memberPackage.setPositiveUnusedAttendanceDay(positiveUnusedClassHour);
                memberPackage.setGiftUnusedAttendanceDay(giftUnusedClassHour);
                memberPackage.setIsAttendClass(true);
            }
            tssMemberPackageDao.updateEntitiy(memberPackage);
        }

       /* this.tssMemberPackageDao.updateParameterById(ImmutableMap.of(
                ("surplusClassHour"), SYsurplusClassHour,
                ("surplusFormalClassHour"), SYsurplusFormalClassHour,
                ("surplusPresentClassHour"), SYsurplusPresentClassHour,
                ("selectedClassHour"), selectedClassHour,
                ("unusedClassHour"), unusedClassHour,
                ("positiveUnusedClassHour"), positiveUnusedClassHour,
                ("giftUnusedClassHour"), giftUnusedClassHour),
                memberPackageId);*/

        //批量保存
        bool = this.dao.jdbcBatchSave(tssMemberAttendClassRecordList);
        if (bool) {
            bool = tssMemberCheckAttendanceDao.jdbcBatchSave(tssMemberCheckAttendanceList);
        }
        /*if(bool){
            bool = tsClassAttendanceDao.jdbcBatchSave(tssClassAttendanceArrayList);
        }*/
        if (!bool) {
            return !bool;
        }

        return bool;
    }

    @Override
    public Integer updateNoClassLessionChangealreadyClass(Integer status, String memberId, String memberPackageId, String targetDataId) {
        return dao.updateNoClassLessionChangealreadyClass(status, memberId, memberPackageId, targetDataId);
    }

    @Override
    public List<TssMemberAttendClassRecord> findAttendClassRecordByLessonsIdMemberId(String memberId, String id) throws Exception {
        return dao.findAttendClassRecordByLessonsIdMemberId(memberId, id);
    }

    @Override
    public List<Map<String, Object>> findAttendClassByClassIdAndMemberPackageId(String memberPackageId, String classId) {
        return dao.findAttendClassByClassIdAndMemberPackageId(memberPackageId, classId);
    }

    @Override
    public List<Map<String, Object>> findCouldClassLessonInfoByClassIdAndMemberPackageId(String memberPackageId, String classId) {
        return dao.findCouldClassLessonInfoByClassIdAndMemberPackageId(memberPackageId, classId);
    }

    @Override
    public List<Map<String, Object>> findCouldClassLessonInfoByClassIdAndMemberPackageId(String classId) {
        return dao.findCouldClassLessonInfoByClassIdAndMemberPackageId(classId);
    }
}
